/*
 * Created by JFormDesigner on Tue Jul 02 01:37:29 CST 2024
 */

package scct.zhang.ui;

import scct.zhang.dao.ContactDao;
import scct.zhang.dao.UserDao;
import scct.zhang.model.User;

import java.util.List;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author 张
 */
public class DlgContactAdd extends JDialog {

    public DlgContactAdd(Window owner) {
        super(owner);
        initComponents();
        initView();
    }

    private void initView() {
        UserDao userDao = new UserDao();
        List<User> allUser = userDao.getAllUser();
        System.out.println(allUser);
        for (User user : allUser) {
            comUser.addItem(user.getUsername());
        }
    }

    private void btnAdd(ActionEvent e) {
        // TODO add your code here
        String name = txtContact.getText();
        String username = (String) comUser.getSelectedItem();
        String relationship = txtRelationship.getText();
        if (name.isEmpty() || !name.matches("[a-zA-Z0-9_\\u4e00-\\u9fa5]{1,10}$")){
            JOptionPane.showMessageDialog(this,"请输入合法用户名");
            return;
        }if (relationship.isEmpty()){
            JOptionPane.showMessageDialog(this,"关系不能为空");
            return;
        }
        ContactDao contactDao = new ContactDao();
        if (contactDao.addContact(name,username,relationship)) {
            JOptionPane.showMessageDialog(this, "添加成功");
        }
        this.dispose();
    }

    private void btnCancle(ActionEvent e) {
        // TODO add your code here
        this.dispose();
    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - zhang
        label3 = new JLabel();
        btnAdd = new JButton();
        btnCancle = new JButton();
        txtRelationship = new JTextField();
        label1 = new JLabel();
        comUser = new JComboBox();
        label4 = new JLabel();
        txtContact = new JTextField();

        //======== this ========
        setTitle("\u6dfb\u52a0\u8054\u7cfb\u4eba");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label3 ----
        label3.setText("\u5173\u7cfb");
        contentPane.add(label3);
        label3.setBounds(80, 135, 35, 17);

        //---- btnAdd ----
        btnAdd.setText("\u6dfb\u52a0");
        btnAdd.addActionListener(e -> btnAdd(e));
        contentPane.add(btnAdd);
        btnAdd.setBounds(new Rectangle(new Point(90, 230), btnAdd.getPreferredSize()));

        //---- btnCancle ----
        btnCancle.setText("\u53d6\u6d88");
        btnCancle.addActionListener(e -> btnCancle(e));
        contentPane.add(btnCancle);
        btnCancle.setBounds(new Rectangle(new Point(250, 230), btnCancle.getPreferredSize()));
        contentPane.add(txtRelationship);
        txtRelationship.setBounds(150, 130, 140, 30);

        //---- label1 ----
        label1.setText("\u7528\u6237\u540d");
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(80, 80), label1.getPreferredSize()));
        contentPane.add(comUser);
        comUser.setBounds(150, 75, 140, 30);

        //---- label4 ----
        label4.setText("\u8054\u7cfb\u4eba");
        contentPane.add(label4);
        label4.setBounds(new Rectangle(new Point(80, 25), label4.getPreferredSize()));
        contentPane.add(txtContact);
        txtContact.setBounds(150, 20, 140, txtContact.getPreferredSize().height);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 345);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - zhang
    private JLabel label3;
    private JButton btnAdd;
    private JButton btnCancle;
    private JTextField txtRelationship;
    private JLabel label1;
    private JComboBox comUser;
    private JLabel label4;
    private JTextField txtContact;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
